﻿using System.Collections.Generic;

namespace ArmyManager.Commands
{
	public class CommandHistory
	{
		private readonly Stack<ICommand> _undoStack = new Stack<ICommand>();
		private readonly Stack<ICommand> _redoStack = new Stack<ICommand>();

		public void ExecuteCommand(ICommand command)
		{
			command.Execute();
			_undoStack.Push(command);
			_redoStack.Clear();
		}

		public ICommand LastCommand
		{
			get { return CanUndo ? _undoStack.Peek() : null; }
		}

		public ICommand NextCommand
		{
			get { return CanRedo ? _redoStack.Peek() : null; }
		}

		public bool CanUndo
		{
			get { return _undoStack.Count > 0; }
		}

		public void Undo()
		{
			if (!CanUndo) return;
			
			ICommand undoCommand = _undoStack.Pop();
			undoCommand.Undo();
			_redoStack.Push(undoCommand);
		}

		public void Redo()
		{
			if (!CanRedo) return;

			ICommand redoCommand = _redoStack.Pop();
			redoCommand.Execute();
			_undoStack.Push(redoCommand);
		}

		public bool CanRedo
		{
			get { return _redoStack.Count > 0; }
		}
	}
}